#ifndef ALARMWIDGET_H
#define ALARMWIDGET_H

#include <QWidget>
#include <QTableWidget>
#include <QTabWidget>
#include <QPushButton>
#include <QComboBox>
#include <QLabel>
#include "utils/AlarmManager.h"

namespace ModbusPlexLink {

class ChannelManager;

/**
 * @brief 报警管理界面
 *
 * 功能：
 * - 活动报警列表
 * - 报警历史查询
 * - 报警规则管理
 * - 报警确认与清除
 * - 报警统计显示
 */
class AlarmWidget : public QWidget {
    Q_OBJECT

public:
    explicit AlarmWidget(AlarmManager* alarmManager, ChannelManager* channelManager, QWidget *parent = nullptr);
    ~AlarmWidget();

    // 刷新显示
    void refreshDisplay();

private slots:
    // 报警信号
    void onAlarmTriggered(const AlarmEvent& event);
    void onAlarmAcknowledged(const QString& eventId);
    void onAlarmCleared(const QString& eventId);

    // 操作
    void onAcknowledgeAlarm();
    void onClearAlarm();
    void onAcknowledgeAll();
    void onClearAll();

    // 规则管理
    void onAddRule();
    void onEditRule();
    void onDeleteRule();
    void onToggleRule();

    // 过滤
    void onPriorityFilterChanged(int index);
    void onRefreshHistory();

private:
    void setupUi();
    void updateActiveAlarmsTable();
    void updateHistoryTable();
    void updateRulesTable();
    void updateStatistics();

    QString alarmTypeToString(AlarmType type) const;
    QString alarmPriorityToString(AlarmPriority priority) const;
    QString alarmStateToString(AlarmState state) const;
    QColor alarmPriorityColor(AlarmPriority priority) const;

private:
    AlarmManager* m_alarmManager;
    ChannelManager* m_channelManager;

    // UI组件
    QTabWidget* m_tabWidget;

    // 活动报警
    QTableWidget* m_activeAlarmsTable;
    QPushButton* m_acknowledgeBtn;
    QPushButton* m_clearBtn;
    QPushButton* m_acknowledgeAllBtn;
    QPushButton* m_clearAllBtn;
    QLabel* m_statsLabel;

    // 报警历史
    QTableWidget* m_historyTable;
    QPushButton* m_refreshHistoryBtn;
    QComboBox* m_historyDaysCombo;

    // 规则管理
    QTableWidget* m_rulesTable;
    QPushButton* m_addRuleBtn;
    QPushButton* m_editRuleBtn;
    QPushButton* m_deleteRuleBtn;
    QPushButton* m_toggleRuleBtn;
    QComboBox* m_priorityFilter;
};

} // namespace ModbusPlexLink

#endif // ALARMWIDGET_H
